package com.ityang.hhhh.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ityang.hhhh.conm.CustomExcept;
import com.ityang.hhhh.conm.R;
import com.ityang.hhhh.dao.UserDao;
import com.ityang.hhhh.domain.User;
import com.ityang.hhhh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Map;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 修改密码
     *
     * @param map
     */
    @Override
    public void updateByIdPass(Map map) {
        //获取邮箱
        String email = (String) map.get("email");
        //获取验证码
        String code = (String) map.get("code");
        //获取用户名
        String username = (String) map.get("username");
        //获取密码
        String password = (String) map.get("password");
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //从Redis取出验证码
        Object emailByRedis =  redisTemplate.opsForValue().get(email);
        String codeByRedis = emailByRedis.toString();
        //比对验证码
        if (!codeByRedis.equals(code)){
            throw new CustomExcept("验证码错误");
        }
        //修改密码
        LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
        luw.set(User::getPassword,password).in(User::getUsername,username);
        this.update(luw);
    }
}
